#include "serverapp.h"
#include <QString>
#include <QFile>
#include <QSettings>
#include <QApplication>

QString ServerApp::AppPath = "";
int     ServerApp::DBPort = 3306;                     //数据库端口
QString ServerApp::DBHost = "localhost";      //数据库服务器名称
QString ServerApp::DBName = "csmobile";           //数据库名称
QString ServerApp::DBUser = "root";           //数据库ProcessID
QString ServerApp::DBPwd = "141230";               //数据库ProcessID密码
QString ServerApp::AppKey = "12345678";
bool    ServerApp::Encryption;

void ServerApp::ReadConfig()
{
    QString fileName = ServerApp::AppPath + "/Server_Config.ini";
    //如果配置文件不存在,则以初始值继续运行,并生成配置文件
    //文件是否存在
    QFile tempFile(fileName);

    if (!tempFile.exists()) {
        WriteConfig();
        return;
    }

    QSettings *set = new QSettings(fileName, QSettings::IniFormat);
    set->beginGroup("AppConfig");

	ServerApp::AppKey = set->value("AppKey").toString();
	ServerApp::DBHost = set->value("DBHost").toString();
	ServerApp::DBPort = set->value("DBPort").toString().toInt();
	ServerApp::DBUser = set->value("DBUser").toString();
	ServerApp::DBPwd =  set->value("DBPwd").toString();
	ServerApp::DBName = set->value("DBName").toString();
	ServerApp::Encryption = set->value("Encryption").toBool();

    set->endGroup();
}

void ServerApp::WriteConfig()
{
    QString fileName = ServerApp::AppPath + "/Server_Config.ini";
    QSettings *set = new QSettings(fileName, QSettings::IniFormat);
    set->beginGroup("AppConfig");

    set->setValue("AppKey", ServerApp::AppKey);
    set->setValue("DBHost", ServerApp::DBHost);
    set->setValue("DBPort", ServerApp::DBPort);
    set->setValue("DBUser", ServerApp::DBUser);
    set->setValue("DBPwd",  ServerApp::DBPwd);
    set->setValue("DBName", ServerApp::DBName);
    set->setValue("Encryption", ServerApp::Encryption);

    set->endGroup();
}

void ServerApp::Load()
{
	ServerApp::AppPath = QApplication::applicationDirPath();
	ServerApp::ReadConfig();
}
